package org.example.web;

import com.github.pagehelper.PageInfo;
import org.example.model.Features;
import org.example.model.Food;
import org.example.model.Result;
import org.example.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //功能是将类声明为一个控制器，并使其中的方法可以直接响应HTTP请求
@RequestMapping("/adminApi/foodCtl")
public class FoodAdminController {
    @Autowired
    private FoodService foodService;

    @RequestMapping("/findList")
    public Result findList(@RequestBody Food food) {
        PageInfo<Food> pageInfo = foodService.getFoodList(food);
        return new Result(200, "success", pageInfo);
    }
    @RequestMapping(path = "/feature", method = RequestMethod.GET)
    public Result feature() {
        System.out.println();
        List<Features> list = foodService.feature();
        return new Result(200, "success", list);
    }
    @RequestMapping(path = "/save", method = RequestMethod.POST)
    public Result addFood(@RequestBody Food food) {
        food.setRating(0.0);
        Food food1 = foodService.addFood(food);
        if (food1 == null) {
            return new Result(500, "添加失败", null);
        }else {
            return new Result(200, "success", food1);
        }

    }
    @RequestMapping(path = "/delete", method = RequestMethod.GET)
    public Result deleteFood(@RequestParam(value = "id") int id) {
        boolean flag = foodService.deleteFood(id);
        if (flag) {
            return new Result(200, "success", null);
        } else {
            return new Result(500, "删除失败", null);
        }
    }

}
